import 'dart:async';
import 'dart:io';

import 'package:my_flu01/page/index_screen_content.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:my_flu01/utils/login.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'common/menu_screen_left.dart';
import 'go_router.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
//
final app = Application();

class DataEvent<T> {
  T? data;

  DataEvent(this.data);

  @override
  String toString() {
    return "DataEvent<$T $data>";
  }
}

class LoginStateEvent extends DataEvent<bool> {
  LoginStateEvent(bool? data) : super(data);
}

class Application  {

  late Login login;
  var routers=router;
  bool _isLogin = false;
  late SharedPreferences spref;
  String _rootRoute = '/';
  late Map<String, WidgetBuilder> routes;
  Map<String, dynamic> pageCaches = {};

  bool initialized = false;

  set isLogin(bool newValue) {
    _isLogin = newValue;
    //router.pushNamed(name)
   // fire(LoginStateEvent(_isLogin));
  }

  bool get isLogin => _isLogin;

  static Future<Application> getInstance() async {
    if (!app.initialized) {
      print("init app ...");
      await app.init();
    }
    return app;
  }

  init() async {
    print("MyAppRouter ...");
    //MyAppRouter();

  }

  void cachePage(String name, dynamic data) {
    pageCaches[name] = data;
  }

  dynamic getPage(String name) {
    return pageCaches.containsKey(name) ? pageCaches[name] : null;
  }
  Future<bool> clearCache() async {
    pageCaches.clear();
    //_siteinfo = null;
    return await spref.clear();
  }

}
/// 外层抽屉菜单
class MenuPage extends StatelessWidget {
  const MenuPage({super.key});

  @override
  Widget build(BuildContext context) {
    final drawerController = ZoomDrawerController();

    return ZoomDrawer(
      controller: drawerController,
      menuScreen: const MenuScreenLeft(), // 抽屉left
      mainScreen: const MainScreenBody(), // 抽屉 body
      borderRadius: 36.w,
      showShadow: true,
      disableDragGesture: false,
      mainScreenTapClose: true,
      openCurve: Curves.easeOut,
      closeCurve: Curves.fastOutSlowIn,
      drawerShadowsBackgroundColor:
      Colors.white38,

     /* menuBackgroundColor: isDarkMode(context)
          ? Theme.of(context).primaryColor.withAlpha(155)
          : Theme.of(context).primaryColor,*/
      angle: 0,
      mainScreenScale: 0.3,
      slideWidth: MediaQuery.of(context).size.width * 0.70,
      style: DrawerStyle.defaultStyle,
    );
  }
}

/// 主屏幕逻辑
class MainScreenBody extends StatelessWidget {
  const MainScreenBody({super.key});

  @override
  Widget build(BuildContext context) {
    /// 监听状态进行改变
    return ValueListenableBuilder<DrawerState>(
      valueListenable: ZoomDrawer.of(context)!.stateNotifier,
      builder: (_, state, child) {
        print('外层菜单状态：$state');
        return AbsorbPointer(
          absorbing: state != DrawerState.closed,
          child: child,
        );
      },
      child: const IndexCreenContent(),
    );
  }
}